from django.shortcuts import render
from celery_client.tasks  import on_task_submitted
# Create your views here.
from rest_framework import  response, viewsets,status
from .models import ShippingContainer,Task
from .serializers import ShippingContainerSerializer,TaskSerializer
class ShippingContainerViewSet(viewsets.ModelViewSet):
    queryset=ShippingContainer.objects.all()
    serializer_class=ShippingContainerSerializer
class TaskViewSet(viewsets.ModelViewSet):
    queryset=Task.objects.all()
    serializer_class=TaskSerializer
    def partial_update(self, request, *args, **kwargs):
        submit_now=request.data.pop('submit',False)
        super().partial_update(request, *args, **kwargs)
        instance=self.get_object()
        if submit_now:
            on_task_submitted.apply([{
                'task_id':instance.id,
                'tenant_id':request.user.tenant.id
            }]).get()
            instance=self.get_object()
        return response.Response(
            self.get_serializer(instance).data,
            status=status.HTTP_200_OK
        )